package com.cuixk.basic.quartz.manager.controller;

import com.cuixk.basic.quartz.manager.bean.QuartzJob;
import com.cuixk.basic.quartz.manager.bean.QuartzTrigger;
import com.cuixk.basic.quartz.manager.bean.dto.ScheduleTaskDTO;
import com.cuixk.basic.quartz.manager.service.ScheduleTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Create By Cuixk
 * 2019/6/21 10:45
 */
@RestController
@RequestMapping("/taskManager")
@Api("定时任务管理")
public class ScheduleTaskManagerController {

    @Resource
    private ScheduleTaskService taskService;

    /**
     * 定时任务列表
     */
    @GetMapping("/listAll")
    @ApiOperation("查看任务列表")
    public List getTaskList() throws SchedulerException {
        return taskService.listScheduleTask();
    }

    /**
     * 定时任务添加
     */
    @PostMapping("/addTask")
    @ApiOperation("添加定时任务")
    public void scheduleList(@ApiParam @RequestBody ScheduleTaskDTO dto) throws SchedulerException {
        taskService.createScheduleTask(dto.getJob(), dto.getTrigger());
    }

    /**
     * 定时任务修改
     */

    /**
     * 定时任务删除
     */

    /**
     * 定时任务启动
     */
    @PostMapping("/run")
    public void runScheduleTask(@ApiParam @RequestBody QuartzTrigger trigger) throws SchedulerException {
        taskService.reRunScheduleTask(trigger);
    }

    /**
     * 定时任务暂停
     */
    @PostMapping("/pauseTask")
    @ApiOperation("暂停 定时任务")
    public void pauseScheduleTask(@ApiParam @RequestBody QuartzJob job) throws SchedulerException {
        taskService.pauseScheduleTask(job);
    }

    /**
     * 定时任务删除
     */
    @DeleteMapping("/deletTask")
    @ApiOperation("删除定时任务")
    public void deleteScheduleTask(@ApiParam @RequestParam String jobName,
                                   @ApiParam @RequestParam String jobGroup) throws SchedulerException {
        taskService.deleteScheduleTask(jobName, jobGroup);
    }
}
